// import type { oDialogPropsType } from './dialog.vue'
import dialogVue from './dialog.vue'
import { createVNode, render } from 'vue'

declare module '@vue/runtime-core' {
  export interface ComponentInternalInstance {
    readonly um: Function[]
  }
}
/**
 * 主动关闭调用返回的 close 方法
 * 主动验证validate，支持validate(['age']) 校验某些字段
 * 可清理验证 app.clearValidate(['age']) 可传某些字段或者不传就是全部
 * @param hook
 * @returns
 */
const open = function (hook: Function) {
  const div = document.createElement('div')
  const vnode = createVNode(dialogVue, {
    setup: hook,
    onUnmounted: () => {
      close()
    }
  })
  render(vnode, div)
  // console.log(vnode)
  const app = vnode.component
  const close = function () {
    app!.um.forEach(fn => {
      fn()
    })
  }
  document.body.appendChild(div)
  return {
    app,
    close,
    validate: app!.exposed!.validate,
    clearValidate: app!.exposed!.clearValidate
  }
}

export default {
  open
}
